package cn.edu.service.impl;

import cn.edu.mapper.InfomationMapper;
import cn.edu.mapper.TeacherMapper;
import cn.edu.pojo.*;
import cn.edu.service.InfomationService;
import cn.edu.service.TeacherService;
import cn.edu.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Davey chan
 */
public  class InfomationServiceImpl implements InfomationService {
    SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();


    @Override
    public InfoPageBean<All> infoselect(int currentPage, int pageSize, String academyName,All t) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
//      获取mapper对象
        InfomationMapper mapper = sqlSession.getMapper(InfomationMapper.class);
//     计算开始的索引
        int begin=(currentPage-1)*pageSize;
//        计算查询的条目数
        int size =pageSize;
//        处理查询条件，模糊查询

        String title = t.getTitle();
        if(title !=null && title.length()>0){
            t.setTitle("%"+title+"%");

        }
        String Researchdir=t.getResearchdir();
        if(Researchdir != null && Researchdir.length()>0){
            t.setResearchdir("%"+Researchdir+"%");
        }
        String Tutor=t.getTutor();
        if(Tutor != null && Tutor.length()>0){
            t.setTutor("%"+Tutor+"%");
        }
        String endTime = t.getEndTime();
        if(endTime != null && endTime.length()>0){
            t.setEndTime("%"+endTime+"%");
        }
        t.setExamstatus("3");
//从前端获取学院的名称，添加学院的限定条件
        academyName="%"+academyName+"%";
        t.setAcademyName(academyName);
//        查询当前页的数据
        List<All> rows = mapper.Infoselect(begin, size,t);
//        查询总记录数
        int totalCount = mapper.selectTotalCountByCondition(t);

//封装pageBean

        InfoPageBean<All> infopageBean=new InfoPageBean<>();
        infopageBean.setRows(rows);
        infopageBean.setTotalCount(totalCount);


        sqlSession.close();
        return infopageBean;
    }

    @Override
    public List<PieData> getpie(String academyName) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
//      获取mapper对象
        InfomationMapper mapper = sqlSession.getMapper(InfomationMapper.class);
        //        封装饼图的数据
        List<PieData> pieData=new ArrayList<>();
        PieData a=new PieData();
        int num1=mapper.GetprofessorNum(academyName);
        a.setValue(num1);
        a.setName("教授");
        pieData.add(a);
        PieData b=new PieData();
        int num2=mapper.GetAsprofessorNum(academyName);
        b.setValue(num2);
        b.setName("副教授");
        pieData.add(b);
        PieData c=new PieData();
        int num3=mapper.GetLecturerNum(academyName);
        c.setValue(num3);
        c.setName("讲师");
        pieData.add(c);
        return pieData;
    }

    @Override
    public List<Integer> getBar(String academyName) {
        List<Integer> barData=new ArrayList<>();
      SqlSession sqlSession = sqlSessionFactory.openSession();
//      获取mapper对象
        InfomationMapper mapper = sqlSession.getMapper(InfomationMapper.class);
        Bardata bardata=new Bardata();
        //        封装柱状图的数据
        int a=mapper.GetOpsNum(academyName);
        bardata.setOpsNum(a);
        int b=mapper.GetPatentNum(academyName);
        bardata.setPatentNum(b);
        int c=mapper.Getresearch_projectNum(academyName);
        bardata.setProjectNum(c);
        int d=mapper.GetthesisNum(academyName);
        bardata.setThesisNum(d);
       barData.add(a);
       barData.add(b);
       barData.add(c);
       barData.add(d);

        return barData;
    }
}
